﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySoft.Data;

namespace Caifu.Datas
{
	/// <summary>
	/// 基金净值
	/// </summary>
	public class NetValueData
	{
		/// <summary>
		/// 
		/// </summary>
		public static readonly NetValueData Instance = new NetValueData();

		private readonly DbSession dbSession = new DbSession("Caifu");

		/// <summary>
		/// 
		/// </summary>
		/// <param name="netValue"></param>
		public void AddNetValue(NetValue netValue)
		{
			netValue.Detach();
			if (netValue.Id == Guid.Empty) netValue.Id = Itfort.Comb.NewComb();
			if (netValue.AddTime == DateTime.MinValue) netValue.AddTime = DateTime.Now;
			if (netValue.UpdateTime == DateTime.MinValue) netValue.UpdateTime = DateTime.Now;
			dbSession.Save<NetValue>(netValue);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="netValue"></param>
		public void UpdateNetValue(NetValue netValue)
		{
			netValue.Attach();
			netValue.UpdateTime = DateTime.Now;
			dbSession.Save<NetValue>(netValue);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="id"></param>
		public void DeleteNetValue(Guid id)
		{
			dbSession.Delete<NetValue>(id);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public NetValue GetNetValue(Guid id)
		{
			return dbSession.Single<NetValue>(id);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="fundId"></param>
		/// <param name="reportDate"></param>
		/// <returns></returns>
		public NetValue GetNetValue(Guid fundId, DateTime reportDate)
		{
			var where = NetValue._.FundId == fundId && NetValue._.ReportDate == reportDate;
			return dbSession.From<NetValue>().Where(where).ToSingle();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="pageIndex"></param>
		/// <param name="pageSize"></param>
		/// <returns></returns>
		public IList<NetValue> GetNetValues(Int32 pageIndex, Int32 pageSize)
		{
			return dbSession.From<NetValue>()
				.OrderBy(NetValue._.ReportDate.Desc)
				.GetPage(pageSize).ToList(pageIndex);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="fundId"></param>
		/// <param name="pageIndex"></param>
		/// <param name="pageSize"></param>
		/// <returns></returns>
		public IList<NetValue> GetNetValues(Guid fundId, Int32 pageIndex, Int32 pageSize)
		{
			var where = NetValue._.FundId == fundId;

			return dbSession.From<NetValue>()
				.Where(where)
				.OrderBy(NetValue._.ReportDate.Desc)
				.GetPage(pageSize).ToList(pageIndex);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="reportDate"></param>
		/// <param name="pageIndex"></param>
		/// <param name="pageSize"></param>
		/// <returns></returns>
		public IList<NetValue> GetNetValues(DateTime reportDate, Int32 pageIndex, Int32 pageSize)
		{
			var where = NetValue._.ReportDate == reportDate;

			return dbSession.From<NetValue>()
				.Where(where)
				.GetPage(pageSize).ToList(pageIndex);
		}
	}
}

